package com.yanqu.road.server.cmd.activity.toyhouse;

import com.yanqu.road.entity.activity.toyhouse.config.ToyHouseConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ToyHouseProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.toyhouse.ToyHouseModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.toyhouse.ToyHouseMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_TOY_HOUSE_GET_GAME_DATA, desc = "获取游戏数据")
public class ToyHouseGetGameDataCmd extends AbstractCommand {

    private int multi;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ToyHouseProto.ToyHouseGetGameDataReqMsg reqMsg = ToyHouseProto.ToyHouseGetGameDataReqMsg.parseFrom(packet.getBytes());
        multi = reqMsg.getMulti();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 活动配置
        ToyHouseConfig config = ToyHouseMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_TOY_HOUSE_NO_ACTIVITY;
            return;
        }

        // 获取数据
        ret = player.getModule(ToyHouseModule.class).syncGameData(config, multi);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            ToyHouseProto.ToyHouseGetGameDataRespMsg.Builder builder = ToyHouseProto.ToyHouseGetGameDataRespMsg.newBuilder();
            builder.setRet(0);
            player.sendPacket(ClientProtocol.U_TOY_HOUSE_GET_GAME_DATA, builder);
        }
    }
}
